<include file="Public/head" />
<include file="Public/common_header" />

<div class="mian">
    <include file="Public/sidebar" menu="cwgl.djq"  />
	<div class="mianR">

        <div class="shangPic"><img src="http://img1.isou365.cn/paimai/yunying/images/yun_pic2.jpg" /></div>
        <div class="shangFrom clearfix">
            <div class="chang-ci">
                <?php if($give_auth===true):?>
                <input type="button" value="发放代金券" id="feiyong1" onclick="window.location.href='/Coupon/assign_coupon'" />
                <?php endif;?>
                <?php if($add_auth===true):?>
                <input type="button" value="新建代金券 " id="xinjian1" />
                <?php endif;?>
            </div>
             <select name="grant_type" id="grant_type" style="height:24px;" onchange="get_grant_coupon_type()">
              <option value="0" selected="selected">请选择发放形式</option>
              <option value="1">手动发放</option>
              <option value="2">自动发放</option>
            </select>
            <div class="zhuang-tai">
                <input type="text" name="s_type" data="" value="" placeholder="请选择类型" readonly />
                <dl id="s_type">
                   <!--  <dd value="">全部</dd>
                    <foreach name="coupon_type" item="item">
                       <if condition="$item.a_id gt 0 ">
                          <dd value="{$item.coupon_type_id}">{$item.type_name}</dd>
                        <else />
                          <dd value="{$item.coupon_type_id}">{$item.type_name}({$item.amount})</dd>
                        </if>
                    </foreach> -->
                </dl>
            </div>
            <div class="zhuang-tai">
                <input type="text" name="s_status" data="" value="" placeholder="请选择发放状态" readonly />
                <dl id="s_status">
                    <dd value="">全部</dd>
                    <dd value="1">待发放</dd>
                    <dd value="2">已发放</dd>
                    <dd value="3">已过期</dd>
                </dl>
            </div>
            <div class="zhuang-tai">
                <input type="text" name="s_use_status" data="" value="" placeholder="请选择使用状态" readonly />
                <dl id="s_use_status">
                    <dd value="">全部</dd>
                    <dd value="1">未使用</dd>
                    <dd value="2">已使用</dd>
                    <dd value="3">已过期</dd>
                </dl>
            </div>
           
        <div class="deng-ji">
            <select id="branch_id" name="branch_id" class="branch_company" style="height:24px;">
                <if condition="count($branch_company) gt 1">
                    <option value="">请选择分公司</option>
                </if>
                <volist name="branch_company" id="branch">
                    <option value="{$key}">{$branch}</option>
                </volist>
            </select>
        </div>
        <div class="deng-ji"><input type="text" name="keyword" value="" placeholder="代金券号、车商账号"/></div>
        <div class="shangSou" style="margin-top:5px;"><input id="searchBtn" type="button" value="搜索" /></div>
        </div>

		<div class="listBox" id="listBox">

		</div>


<!--新建弹出窗口-->
       <div class="yunBei"></div>
       <div class="tanShang tanShang5">
           <h3>新建代金券</h3>
           <ul class="daiJingW">
               <li class="clearfix">
                   <span class="daiJing">代金券类型</span>
                   <input type="text" name="type" value="开店赠送" placeholder="请输入代金券类型" />
               </li>
               <li class="clearfix">
                   <span class="daiJing">券面金额</span>
                   <input type="text" name="amount" value="100" placeholder="请输入发放金额"/>
               </li>
               <li class="clearfix">
                   <span class="daiJing">数量</span>
                   <input type="text" name="num" value="50" placeholder="请输入代金券数量"  />
               </li>
               <li class="clearfix">
                   <span class="daiJing">有效期</span>
                   <input type="text" name="start_date" id="demoText3" value="<?php echo date('Y-m-d');?>"  placeholder="请选择起始日期" class="riqi" readonly />
               </li>
               <li class="clearfix">
                   <input type="text" name="end_date" value="<?php echo date('Y-m-d',strtotime('next month'));?>" placeholder="请选择截止日期" id="demoText4" class="riqi riqi1" readonly />
               </li>
               <li class="clearfix">
                   <span class="daiJing">所属公司</span>
                   <select id="branch_id2" name="branch_id2" class="branch_company">
                       <if condition="count($branch_company) gt 1">
                           <option value="">请选择分公司</option>
                       </if>
                       <volist name="branch_company" id="branch">
                           <option value="{$key}">{$branch}</option>
                       </volist>
                   </select>
               </li>
               <li class="clearfix">
                   <input type="button" id="addCoupon" value="确定" class="queding" />
                   <input type="button" value="取消" class="quxiao" />
               </li>
           </ul>
       </div>
		<div class="clear"></div>
	</div>
</div>
<script type="text/javascript">
	get_list();
	function get_list(){
        var url = mkurl();
	    $.get(url,function(data){
	    	$("#listBox").html(data);
	    },"html");
	}

	function get_page(p){
        var url = mkurl();
	    $.get(url, {'p':p}, function(data){
	    	$("#listBox").html(data);
	    },"html");
	}
    //赋值
    // $('#s_type > dd').on('click',function(){
    //     $('input[name=s_type]').attr('data',$(this).attr('value'));
    // });
    $('#s_type').delegate('dd','click',function(){
        $('input[name=s_type]').attr('data',$(this).attr('value'));
        $('input[name=s_type]').val($(this).html());
    });
    $('#s_status > dd').on('click',function(){
        $('input[name=s_status]').attr('data',$(this).attr('value'));
    });
    $('#s_use_status > dd').on('click',function(){
        $('input[name=s_use_status]').attr('data',$(this).attr('value'));
    });

    //搜索
    $("#searchBtn").on('click',function(){
        var url = mkurl();
        $.get(url,function(data){
   	    	$("#listBox").html(data);
   	    },"html");
    });
    function mkurl(){
        var url = '/Coupon/coupon_list';
        var type = $('input[name=s_type]').attr('data');
        if(type){
            url += '/type/'+type;
        }

        var status = $('input[name=s_status]').attr('data');
        if(status){
            url += '/status/'+status;
        }

        var use_status = $('input[name=s_use_status]').attr('data');
        if(use_status){
            url += '/use_status/'+use_status;
        }
//        var bidding_start_time = $('input[name=bidding_start_time]').val();
//        if(bidding_start_time && bidding_start_time !="请选择竞拍日期"){
//            url += '/bidding_start_time/'+bidding_start_time;
//        }
        var grant_type = $('#grant_type option:selected').val();
        if (grant_type) {
          url += '/grant_type/'+grant_type;
        };
        var keyword = $('input[name=keyword]').val();
        if(keyword && keyword !="代金券号、车商账号"){
            url += '/keyword/'+encodeURIComponent(keyword);
        }

        var branch_id = $('select[name=branch_id]').val();
        if(branch_id){
            url += '/branch_id/'+branch_id;
        }

        return url;
    }

    $(".zhuang-tai").eq(2).hide();
    $("#s_status dd").click(function(){

        if($(this).text()!="已发放"){
            $(".zhuang-tai").eq(2).hide();
            $(".deng-ji").eq(0).css("marginTop","0px");
            $(".deng-ji").eq(1).css("marginTop","0px");
            $(".shangSou").css("marginTop","5px");
        }else{
            $(".zhuang-tai").eq(2).show();
            $(".deng-ji").eq(0).css("marginTop","5px");
            $(".deng-ji").eq(1).css("marginTop","5px");
            $(".shangSou").css("marginTop","5px");
        }


    })

    $('.queding').on('click',function(){
        var type = $('input[name=type]').val();
        if(type == ''){
            shibai_alert('请输入代金券类型');
            return;
        }
        var amount = $('input[name=amount]').val();
        if(amount == ''){
            shibai_alert('请输入代金券面额');
            return;
        }
        var num = $('input[name=num]').val();
        if(num == ''){
            shibai_alert('请输入创建代金券的数量');
            return;
        }
        var start_date = $('input[name=start_date]').val();
        if(start_date == ''){
            shibai_alert('请选择代金券生效开始日期');
            return;
        }
        var end_date = $('input[name=end_date]').val();
        if(end_date == ''){
            shibai_alert('请选择代金券生效结束日期');
            return;
        }

        var branch_id = $('select[name=branch_id2]').val();
        if(branch_id == ''){
            shibai_alert('请选择所属公司');
            return;
        }

        var param={
             'type':type,
            'amount':amount,
            'num':num,
            'start_date':start_date,
            'end_date':end_date,
            'branch_id':branch_id
        };
        var btn = $("#addCoupon");
        btn.attr("disabled", "disabled").css({"background": "#999", "border": "none"}).val("提交中...");
        $.post("/Coupon/add",param,function(data){
            if(data.code=='200'){
                $('.tanShang5').hide();
                $('.yunBei').hide();
                btn.removeAttr("disabled style").val("确定")
                btn_alert(data.msg,2000,function(){window.location.reload()});
            }else{
                shibai_alert(data.msg);
            }

        },'json');
    });

function get_grant_coupon_type(){
  var grant_type = $('#grant_type').children('option:selected').val();
   $.post("/Coupon/get_grant_coupon_type",{'grant_type':grant_type},function(data){
      var html = '';
      $.each(data,function(i,n){
          html += "<dd value="+n.coupon_type_id+">"+n.type_name+"</dd>";
      });
      $('#s_type').html(html);
      },'json');
}
get_grant_coupon_type();
</script>
<include file="Public/foot" />